package com.winsdom.service.impl;

import com.winsdom.entity.Customer;
import com.winsdom.mapper.CustomerMapper;
import com.winsdom.service.CustomerService;
import com.winsdom.vo.CustomerQueryVO;
import com.winsdom.context.BaseContext;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Map;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 客户主表(公共字段) 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-05-12
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Map<String, Object> queryCustomers(CustomerQueryVO query) {
        List<Map<String, Object>> list = customerMapper.selectCustomerWithUpdateUserName(query);
        Map<String, Object> result = new HashMap<>();
        result.put("data", list);
        return result;
    }

    @Override
    public Map<String, Object> updateLegalProcedure(Long customerId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Assert.notNull(customerId, "客户ID不能为空");
            
            // 从BaseContext获取当前用户ID
            Long currentUserId = BaseContext.getCurrentId().longValue();
            Assert.notNull(currentUserId, "获取当前用户ID失败");
            
            customerMapper.updateLegalProcedure(customerId, currentUserId);
            result.put("success", true);
            result.put("message", "更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }
    
    @Override
    public Map<String, Object> updateCustomerStatus(Long customerId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Assert.notNull(customerId, "客户ID不能为空");
            
            // 从BaseContext获取当前用户ID
            Long currentUserId = BaseContext.getCurrentId().longValue();
            Assert.notNull(currentUserId, "获取当前用户ID失败");
            
            customerMapper.updateCustomerStatus(customerId, currentUserId);
            result.put("success", true);
            result.put("message", "状态更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "状态更新失败：" + e.getMessage());
        }
        return result;
    }
}
